import http from '@/common/utils/http';
import store from '@/store';
import fontJson from '@/tool/tmicon/fontJson';
import systemConfig from '@/common/config/index';
import preview, * as util from '@/tool/function/util';
import { language, languageByGlobal } from '@/tool/lib/language';
import { share } from '@/tool/lib/share';
import { App, nextTick } from 'vue';
import tmuiconfigdefault from '@/tool/lib/tmuiconfigDefault';
import PageJsonInit from './pages.json';
let pages = new Map<string, pagesType>();
if (typeof PageJsonInit?.pages == 'undefined') {
  PageJsonInit.pages = [];
}
PageJsonInit.pages.forEach((el: any) => {
  let customType: pagesCustomType = <pagesCustomType>(el?.style?.navigationStyle ?? 'default');
  let bg = (el.style?.navigationBarBackgroundColor ?? PageJsonInit?.globalStyle?.navigationBarBackgroundColor ?? '#FFFFFF') || '#FFFFFF';
  let txtColor = (el.style?.navigationBarTextStyle ?? PageJsonInit?.globalStyle?.navigationBarTextStyle ?? 'black') || 'black';
  pages.set(el.path, {
    path: el.path,
    name: el.name,
    title: el.style?.navigationBarTitleText ?? systemConfig.systemName,
    meta: el.meta,
    custom: customType,
    navigationBarBackgroundColor: bg,
    navigationBarTextStyle: txtColor,
  });
});
if (Array.isArray(PageJsonInit?.subPackages ?? null)) {
  PageJsonInit?.subPackages.forEach((el: any) => {
    let rootPath = el.root;
    el.pages.forEach((el2: any) => {
      let elany: any = el2;
      let path = rootPath + '/' + elany.path;
      let bg = (el2.style?.navigationBarBackgroundColor ?? PageJsonInit?.globalStyle?.navigationBarBackgroundColor ?? '#FFFFFF') || '#FFFFFF';
      let txtColor = (el2.style?.navigationBarTextStyle ?? PageJsonInit?.globalStyle?.navigationBarTextStyle ?? 'black') || 'black';
      pages.set(path, {
        path: path,
        name: elany.name,
        title: elany.style?.navigationBarTitleText ?? systemConfig.systemName,
        meta: elany.meta,
        custom: elany?.style?.navigationStyle ?? 'default',
        navigationBarBackgroundColor: bg,
        navigationBarTextStyle: txtColor,
      });
    });
  });
}
let pagers: any = PageJsonInit;
let tabBar: tabBarType = pagers?.tabBar ?? {
  color: '',
  selectedColor: '',
  borderStyle: '',
  backgroundColor: '',
  list: [],
};

// custom icon
let cusutomIconList = [];
// #ifdef APP
cusutomIconList = fontJson;
// #endif
let $tm: any = {
  tabBar: tabBar,
  pages: pages,
  globalNavStyle: PageJsonInit?.globalStyle.navigationStyle ?? '',
  isOpenDarkModel: (PageJsonInit?.globalStyle?.navigationBarBackgroundColor ?? '').indexOf('@') > -1,
  isColor: (color: string) => {
    const reg1 = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
    const reg2 = /^(rgb|RGB|rgba|RGBA)/;
    return reg1.test(color) || reg2.test(color);
  },
  u: { ...util, preview },
  language: language,
  http: http,
  store: store,
  tmicon: [
    {
      font: 'tmicon',
      prefix: 'tmicon-',
      fontJson: cusutomIconList,
    },
  ],
  config: tmuiconfigdefault as any,
};

export default {
  install: (app: App, options: object) => {
    // #ifndef APP-NVUE
    app.use(languageByGlobal());
    // #endif
    let appconfig = {};
    // #ifdef MP
    if (!$tm.config.shareDisable) {
      const { onShareAppMessage, onShareTimeline } = share();
      appconfig = { ...appconfig, onShareAppMessage, onShareTimeline };
    }
    // #endif

    app.mixin({
      ...appconfig,
    });
    /**合并插件的全局配置 */
    $tm = {
      ...$tm,
      config: options,
    };

    /**对外暴露 */
    uni.$tm = $tm;
    // #ifdef APP-VUE
    uni.setStorageSync('$zhTheme', JSON.stringify($tm.config.theme));
    // #endif
    app.config.globalProperties.tm = $tm;
  },
};
